package com.mybatis.generate.generator;

import java.io.IOException;
import java.util.Properties;

import com.mybatis.generate.util.PropertiesHelper;

/**
 * 用户装载generator.properties文件
 * @author yanwl
 *
 */
public class GeneratorProperties {
	private static final String PROPERTIES_FILE_NAME = "generator.properties";
	private static PropertiesHelper props;
	
	private GeneratorProperties(){
		
	}
	
	public static Properties getProperties() {
		return getHelper().getProperties();
	}

	private static PropertiesHelper getHelper() {
		if(props == null) {
			loadProperties();
		}
		return props;
	}

	private static void loadProperties() {
		props = new PropertiesHelper(loadProperties(PROPERTIES_FILE_NAME));
		/*String basePackage = props.getRequiredProperty("basepackage");
		String basepackage_dir = basePackage.replace('.', '/');
		props.setProperty("basepackage_dir", basepackage_dir);*/
	}

	private static Properties loadProperties(
			String resourcesName) {
		Properties properties = new Properties();
		try {
			properties.load(GeneratorProperties.class.getResourceAsStream(resourcesName));
//			properties.load(GeneratorProperties.class.getClassLoader().getResourceAsStream("com/mybatis/generate/generator/"+resourcesName));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return properties;
	}
	
	public static String getProperty(String key, String defaultValue) {
		return getHelper().getProperty(key, defaultValue);
	}
	
	public static String getProperty(String key) {
		return getHelper().getProperty(key);
	}
	
	public static String getRequiredProperty(String key) {
		return getHelper().getRequiredProperty(key);
	}
	
	public static int getRequiredInt(String key) {
		return getHelper().getRequiredInt(key);
	}
	
	public static boolean getRequiredBoolean(String key) {
		return getHelper().getRequiredBoolean(key);
	}
	
	public static String getNullIfBlank(String key) {
		return getHelper().getNullIfBlank(key);
	}
	
	public static void setProperty(String key,String value) {
		getHelper().setProperty(key, value);
	}
	
	public static void setProperties(Properties v) {
		props = new PropertiesHelper(v);
	}
	
	public static void main(String[] args) {
		loadProperties();
	}
}
